package easycloud.ecs.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import easycloud.ecs.web.exception.DomainNotFoundException;
import easycloud.ecs.web.exception.SessionNotFoundException;

@Controller
@RequestMapping("error")
public class ExceptionController {

	private static final Logger logger = LoggerFactory
			.getLogger(ExceptionController.class);

	@ExceptionHandler(IllegalArgumentException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public ModelAndView handleIllegalArgumentException(Exception exception) {
		ExceptionController.logger.warn(exception.getMessage());
		ModelAndView mav = new ModelAndView("comm.exception");
		mav.addObject("exception", exception);
		return mav;
	}

	@ExceptionHandler(value = DomainNotFoundException.class)
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public ModelAndView handleDomainNotFoundException(
			DomainNotFoundException exception) {
		ExceptionController.logger.warn(exception.getMessage());
		ModelAndView mav = new ModelAndView("comm.exception");
		mav.addObject("exception", exception);
		return mav;
	}

	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public ModelAndView handleInternalServerError(Exception exception) {
		ExceptionController.logger.warn(exception.getMessage());
		ModelAndView mav = new ModelAndView("comm.exception");
		mav.addObject("exception", exception);
		return mav;
	}

	@ExceptionHandler(value = SessionNotFoundException.class)
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public ModelAndView handleSessionNotFoundException(
			SessionNotFoundException exception) {
		ExceptionController.logger.warn(exception.getMessage());
		ModelAndView mav = new ModelAndView("comm.exception");
		mav.addObject("exception", exception);
		return mav;
	}

	@RequestMapping(value = "accessdeny", method = RequestMethod.GET)
	public String pageAccessDeny() {
		ExceptionController.logger.debug("AccessDeny");
		return "error.accessdeny";
	}

	@RequestMapping(value = "invalidsession", method = RequestMethod.GET)
	public String pageInvalidSession() {
		ExceptionController.logger.debug("InvalidSession");
		return "error.invalidsession";
	}
}
